import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/widgets/get_view_keep_alive.dart';
import 'package:vape_xe/widgets/widgets.dart';

import 'logic.dart';

class ProceedsListView extends GetViewKeepAlive<ProceedsController> {
  ProceedsListView({super.key, required super.tag}) {
    Get.put(ProceedsController(tag!), tag: tag);
  }

  Widget _inviteItem(int index) {
    return Container(
      width: 300.w,
      height: 64.h,
      decoration: BoxDecoration(
        borderRadius: 12.radius,
        color: Colors.white10,
      ),
      margin: EdgeInsets.only(left: 16.w, bottom: 12.h, right: 16.w),
      child: Row(
        children: [
          16.vGap,
          netImageCached(
            controller.proceedsList[index]['avatar'],
            width: 42.w,
            height: 42.w,
            radius: 100.w,
            fit: BoxFit.cover,
          ),
          12.vGap,
          Text(
            controller.proceedsList[index]['nickName'],
            style: TextStyle(
              fontSize: 12.sp,
              fontFamily: 'Montserrat',
              fontWeight: FontWeight.bold,
            ),
          ),
          const Spacer(),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Padding(
                padding: EdgeInsets.only(bottom: 3.h),
                child: Text(
                  '+${ConfigStore.to.currency}',
                  style: TextStyle(
                    fontSize: 11.sp,
                    fontFamily: 'Montserrat',
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Text(
                controller.proceedsList[index]['money'].toString().removeZero(),
                style: TextStyle(
                  fontSize: 16.sp,
                  fontFamily: 'Montserrat',
                  fontWeight: FontWeight.bold,
                ),
              )
            ],
          ),
          16.vGap
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullUp: true,
      physics: const BouncingScrollPhysics(),
      controller: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: Obx(
        () => controller.proceedsList.isEmpty
            ? SizedBox(
                height: 200.h,
                child: Center(
                  child: Text('Empty list'.tr),
                ),
              )
            : ListView.builder(
                itemCount: controller.proceedsList.length,
                shrinkWrap: true, // 防止 ListView 自身滚动
                physics: const NeverScrollableScrollPhysics(), // 禁止 ListView 自身滚动
                itemBuilder: (context, index) => _inviteItem(index),
              ),
      ),
    );
  }
}
